package cn.bdqn.service.questionBank;

import cn.bdqn.dao.questionBank.AccountTypeDao;
import cn.bdqn.dao.questionBank.OperationRecordDao;
import cn.bdqn.dto.questionBank.subject.SubjectAccountTypeDTO;
import cn.bdqn.pojo.questionBank.AccountType;
import cn.bdqn.pojo.questionBank.OperationRecord;
import cn.bdqn.utils.Json;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("accountTypeService")
public class AccountTypeService {

    @Autowired
    private AccountTypeDao accountTypeDao;

    @Autowired
    private OperationRecordDao operationRecordDao;

    @Autowired
    private IndustryService industryService;

    @Autowired
    private OperationRecordService operationRecordService;
    /**
     * 判断有没有该科目类型的试卷和题目
     * 没有就直接删除
     * @param accountTypeId
     * @return boolean
     */
    @Transactional(rollbackFor = Exception.class)
    public Json selectAccountTypeNumberOfSubjectAndTestPaper(Integer accountTypeId){
        //System.out.println(accountTypeId);
        List<Integer> integerList = accountTypeDao.selectAccountTypeNumberOfSubjectAndTestPaper(accountTypeId);
        if(accountTypeId == null){
            return Json.fall("没有接收到浏览器传过来的参数");
        }
        if(integerList == null || integerList.size() != 2){
            return Json.fall("数据库没有返回正常数据");
        }

        if(integerList.get(0) <= 0 && integerList.get(1) <= 0){
            int a = accountTypeDao.deleteAccountTypeByAccountTypeId(accountTypeId);
            if(a != 1){
                return Json.fall("没删除成功");
            }
        }
        return industryService.selectIndustryAccountType();
    }

    /**
     * 科目的修改操作，还有修改记录的操作
     * @param subjectAccountTypeDTO
     * @param userId
     * @return AccountType
     */
    @Transactional(rollbackFor = Exception.class)
    public Json updateAccountType(SubjectAccountTypeDTO subjectAccountTypeDTO,Integer userId) throws Exception {
        if(subjectAccountTypeDTO == null){
            return Json.fall("浏览器没有传参数");
        }


        AccountType accountType = new AccountType();
        OperationRecord operationRecord = new OperationRecord();

        operationRecord.setUserId(userId);
        operationRecord.setOperationRecordDescribe("修改操作");
        operationRecord.setOperationRecordRemarks(subjectAccountTypeDTO.getOperationRecordRemarks());
        operationRecord.setOperationRecordTime(subjectAccountTypeDTO.getTime());

        int a = operationRecordDao.insert(operationRecord);
        System.out.println(a);

        accountType.setAccountTypeId(subjectAccountTypeDTO.getId());
        accountType.setAccountTypeName(subjectAccountTypeDTO.getName());
        accountType.setIndustryId(subjectAccountTypeDTO.getTypeId());
        accountType.setUpdateOperationRecordId(operationRecord.getOperationRecordId());



        UpdateWrapper<AccountType> accountTypeUpdateWrapper = new UpdateWrapper<>();
        accountTypeUpdateWrapper
                .eq("account_type_id",subjectAccountTypeDTO.getId());
        int b = accountTypeDao.update(accountType,accountTypeUpdateWrapper);
        if(a == 1&& b == 1){
            return industryService.selectIndustryAccountType();
        }else{
            throw new Exception("添加失败");
        }
    }

    /**
     * 添加一个科目
     * @param subjectAccountTypeDTO
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Json insertAccountType(SubjectAccountTypeDTO subjectAccountTypeDTO,Integer userId) throws Exception {
        OperationRecord operationRecord = new OperationRecord();
        operationRecord.setOperationRecordTime(subjectAccountTypeDTO.getTime());
        operationRecord.setOperationRecordRemarks(subjectAccountTypeDTO.getOperationRecordRemarks());
        operationRecord.setOperationRecordDescribe("添加操作");
        operationRecord.setUserId(userId);

        if(operationRecordService.insertOperationRecord(operationRecord) == null){
            throw new Exception("添加操作表失败失败");
        }

        AccountType accountType = new AccountType();
        accountType.setAccountTypeName(subjectAccountTypeDTO.getName());
        accountType.setIndustryId(subjectAccountTypeDTO.getTypeId());
        accountType.setCreateOperationRecordId(operationRecord.getOperationRecordId());
        accountType.setUpdateOperationRecordId(0);

        int a = accountTypeDao.insert(accountType);
        if(a != 1){
            throw new Exception("添加科目表失败失败");
        }
        return industryService.selectIndustryAccountType();
    }

    /**
     * 查询所有行业和科目信息
     * @return
     */
    public Json selectIndustryAccountType(){
        return Json.ok(accountTypeDao.selectIndustryAccountType());
    }
}
